﻿using NDF.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using UA.Framework;
using UA.Framework.Authorization;

namespace UA.Web.Filters
{
    /// <summary>
    /// 用于验证页面 Controller/Action 访问请求登录状态的过滤器。
    /// </summary>
    public class LoginStatusAuthorizeAttribute : NDF.Web.Mvc.AuthorizeAttribute
    {

        /// <summary>
        /// 验证当前请求是否经过有效的登录。
        /// </summary>
        /// <param name="filterContext"></param>
        /// <returns></returns>
        protected override bool AuthorizeCore(AuthorizationContext filterContext)
        {
            return UAAuthentication.IsAuthenticated;
        }

        /// <summary>
        /// 当页面请求被判断为 未登录 时，所执行的动作（跳转到登录页面）。
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            string loginUrl = UAAuthentication.LoginUrl,
                originalUrl = filterContext.HttpContext.Request.Path,
                suffix = (loginUrl.Contains("?") ? "&&" : "?") + "returnUrl=" + originalUrl;
            filterContext.Result = new RedirectResult(loginUrl + suffix);
        }
    }
}
